﻿using MediatR;
using T_Rbac_APi.Application.Command.DepartmentCommand;
using T_Rbac_Rep.Interface;

namespace T_Rbac_APi.Application.CommandHandler.DepartmentHandler
{
    public class DepartmentDeleteListHandlers : IRequestHandler<DepartmentDeleteListCommand, int>
    {
        private readonly IDapartment dapartment;

        public DepartmentDeleteListHandlers(IDapartment dapartment)
        {
            this.dapartment = dapartment;
        }

        public async Task<int> Handle(DepartmentDeleteListCommand request, CancellationToken cancellationToken)
        {
            var Id = (!string.IsNullOrEmpty(request.ids)) ? request.ids.Split(',').Select(x => Convert.ToInt32(x)).ToArray() : null;
            var lists = await dapartment.GetvaluesByResult(x => Id.Contains(x.Did));
            if (Id!=null)
            {
                foreach (var item in lists)
                {
                    item.IsDel = true;
                    item.UpdateDate = DateTime.Now;
                    item.UpdateBy = "小明";
                    await dapartment.Update(item);
                }
                return await Task.FromResult(1);
            }
            return await Task.FromResult(-1);
        }
    }
}
